﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;
using System.Windows.Forms.DataVisualization.Charting;

namespace QLNhanKhau
{
    public partial class UCBaoCao : UserControl
    {
        public UCBaoCao()
        {
            InitializeComponent();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            
        }

        private void UCBaoCao_Load(object sender, EventArgs e)
        {
            List<DanTocPublic> dt = DanTocBLL.LoadBieuDoDanToc();
            chart1.Series["DanToc"].Points.DataBindXY(dt, "MaDT", dt, "SL");
            //chart1.Data
            for (int i = 0; i < chart1.Series[0].Points.Count(); i++)
            {
                chart1.Series[0].Points[i].LegendText = dt[i].TenDanToc + ": #VALY";
                //chart1.Series[0].Points[i].Label = ;
            }
            chart1.Legends.Add(new Legend("Stores"));
            chart1.Series[0].ChartType = SeriesChartType.Pie;
            chart1.Series[0]["PieLabelStyle"] = "Outside";
            chart1.Series[0]["PieLineColor"] = "Black";

            chart1.Series[0].Label = "#PERCENT{P0}";
            chart1.Series[0].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            chart1.Series[0].ChartType = SeriesChartType.Pie;
            chart1.Series[0]["PieLabelStyle"] = "Outside";
            chart1.Series[0].Legend = "Stores";
            chart1.Legends[0].Docking = Docking.Bottom;
            //char ton giáot

            List<TonGiaoPublic> tg = TonGiaoBLL.LoadBieuDoTonGiao();
            chart2.Series["TonGiao"].Points.DataBindXY(tg, "MaTG", tg, "SL");
            //chart1.Data
            for (int i = 0; i < chart2.Series[0].Points.Count(); i++)
            {
                chart2.Series[0].Points[i].LegendText = tg[i].TenTonGiao + ": #VALY";
                //chart1.Series[0].Points[i].Label = ;
            }
            chart2.Legends.Add(new Legend("Stores"));
            chart2.Series[0].ChartType = SeriesChartType.Pie;
            chart2.Series[0]["PieLabelStyle"] = "Outside";
            chart2.Series[0]["PieLineColor"] = "Black";

            chart2.Series[0].Label = "#PERCENT{P0}";
            chart2.Series[0].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            chart2.Series[0].ChartType = SeriesChartType.Pie;
            chart2.Series[0]["PieLabelStyle"] = "Outside";
            chart2.Series[0].Legend = "Stores";
            chart2.Legends[0].Docking = Docking.Bottom;
            //loat bieu do trinh do 
            List<TrinhDoPublic> td = TrinhDoBLL.LoadBieuDoTrinhDo();
            chart3.Series["TrinhDo"].Points.DataBindXY(td, "MaTD", td, "SL");
            //chart1.Data
            for (int i = 0; i < chart3.Series[0].Points.Count(); i++)
            {
                chart3.Series[0].Points[i].LegendText = td[i].TenTrinhDo + ": #VALY";
                //chart1.Series[0].Points[i].Label = ;
            }
            chart3.Legends.Add(new Legend("Stores"));
            chart3.Series[0].ChartType = SeriesChartType.Pie;
            chart3.Series[0]["PieLabelStyle"] = "Outside";
            chart3.Series[0]["PieLineColor"] = "Black";

            chart3.Series[0].Label = "#PERCENT{P0}";
            chart3.Series[0].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            chart3.Series[0].ChartType = SeriesChartType.Pie;
            chart3.Series[0]["PieLabelStyle"] = "Outside";
            chart3.Series[0].Legend = "Stores";
            chart3.Legends[0].Docking = Docking.Bottom;

            //load tuoi

            List<TuoiPulic> t = TuoiBLL.LoadBieuDoTuoi();
            chart4.Series["Tuoi"].Points.DataBindXY(t, "MaT", t, "SL");
            //chart1.Data
            for (int i = 0; i < chart4.Series[0].Points.Count(); i++)
            {
                chart4.Series[0].Points[i].LegendText = t[i].MucTuoi + ": #VALY";
                //chart1.Series[0].Points[i].Label = ;
            }
            chart4.Legends.Add(new Legend("Stores"));
            chart4.Series[0].ChartType = SeriesChartType.Pie;
            chart4.Series[0]["PieLabelStyle"] = "Outside";
            chart4.Series[0]["PieLineColor"] = "Black";

            chart4.Series[0].Label = "#PERCENT{P0}";
            chart4.Series[0].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            chart4.Series[0].ChartType = SeriesChartType.Pie;
            chart4.Series[0]["PieLabelStyle"] = "Outside";
            chart4.Series[0].Legend = "Stores";
            chart4.Legends[0].Docking = Docking.Bottom;
            
            //this.reportViewer1.RefreshReport();



            for (int i = 2005; i <= DateTime.Now.Year; i++)
            {

                cmbThoiGianBatDau.Items.Add(i.ToString());
                cmbThoiGianKetThuc.Items.Add(i.ToString());
            }
            cmbThoiGianBatDau.SelectedIndex = -1;
            cmbThoiGianBatDau.Text = "Bắt đầu";
            cmbThoiGianKetThuc.SelectedIndex = -1;
            cmbThoiGianKetThuc.Text = "Đến nay";
            //MessageBox.Show(cmbThoiGianBatDau.Text.ToString());

            txtSoSoHoKhau.Text = SoHoKhauBLL.CountSoHoKhau().ToString();
            txtSoNhanKhau.Text = NhanKhauBLL.CountNhanKhau().ToString();
            txtNKNam.Text = NhanKhauBLL.CountNhanKhauNam().ToString();
            txtNKNu.Text = NhanKhauBLL.CountNhanKhauNu().ToString();
            txtChuyenDen.Text = ChuyenKhauBLL.CountChuyenDenByYear(2005, DateTime.Now.Year).ToString();
            txtChuyenDi.Text = ChuyenKhauBLL.CountChuyenDiByYear(2005, DateTime.Now.Year).ToString();
            txtKhaiSinh.Text = KhaiSinhBLL.CountKhaiSinhByYear(2005, DateTime.Now.Year).ToString();
            txtKhaiTu.Text = KhaiTuBLL.CountKhaiTuByYear(2005, DateTime.Now.Year).ToString();
            //this.reportViewer1.RefreshReport();


        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            //var tgbt = dateBatDau.Value;
            //var tgkt = dateKetThuc.Value;
            //this.Age.Series["Nam"].Points.AddXY("A", 11);
            //this.Age.Series["Nam"].Points.AddXY("d", 20);
            //this.Age.Series["Nam"].Points.AddXY("g", 23);
            //this.Age.Series["Nam"].Points.AddXY("s", 34);
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            int start;
            int and;
            if (cmbThoiGianBatDau.SelectedIndex == -1)
            {
                start = 2005;
            }
            else
            {
                start = int.Parse(cmbThoiGianBatDau.Text);
            }
            if (cmbThoiGianKetThuc.SelectedIndex == -1)
            {
                and = DateTime.Now.Year;
            }
            else
            {
                and = int.Parse(cmbThoiGianKetThuc.Text);
            }
            txtSoSoHoKhau.Text = SoHoKhauBLL.CountSoHoKhauByYear(start, and).ToString();
            txtChuyenDen.Text = ChuyenKhauBLL.CountChuyenDenByYear(start, and).ToString();
            txtChuyenDi.Text = ChuyenKhauBLL.CountChuyenDiByYear(start, and).ToString();

            txtKhaiSinh.Text = KhaiSinhBLL.CountKhaiSinhByYear(start, and).ToString();
            txtKhaiTu.Text = KhaiTuBLL.CountKhaiTuByYear(start, and).ToString();

            txtSoNhanKhau.Text=(int.Parse(txtKhaiSinh.Text)-int.Parse(txtKhaiTu.Text)+int.Parse(txtChuyenDen.Text)-ChuyenKhauBLL.CountChuyenDinByYear(start,and)).ToString();
            //txtNKNam.Text

        }

        private void cmbThoiGianBatDau_SelectionChangeCommitted(object sender, EventArgs e)
        { 
            cmbThoiGianKetThuc.Items.Clear();
            for (int i = int.Parse(cmbThoiGianBatDau.SelectedItem.ToString()); i <= DateTime.Now.Year; i++)
            { 
                cmbThoiGianKetThuc.Items.Add(i.ToString());
            }
            //MessageBox.Show(cmbThoiGianKetThuc.SelectedIndex.ToString());

            //MessageBox.Show(cmbThoiGianKetThuc.SelectedText.ToString());

            //if (Int32.Parse(cmbThoiGianBatDau.SelectedText) > Int32.Parse(cmbThoiGianKetThuc.SelectedText))
            //{
            //    cmbThoiGianKetThuc.SelectedIndex = 0;
            //}

        }

      
    }
}
